package logic

import (
	"api/internal/svc"
	"api/internal/types"
	"api/work/global"
	"api/work/model"
	"context"
	"goods_srv/goods"

	"github.com/zeromicro/go-zero/core/logx"
)

type SearchLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSearchLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchLogic {
	return &SearchLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SearchLogic) Search(req *types.SearchRequest) (resp *types.Response, err error) {
	search, err := l.svcCtx.GoodsSrv.GoodsSearch(l.ctx, &goods.GoodsSearchReq{GoodsName: req.GoodsName})
	if err != nil {
		return &types.Response{
			Code: -1,
			Meg:  "建立链接失败",
			Data: nil,
		}, nil
	}

	var goodsList []model.Goods2

	db := global.Db.Table("goods2")

	db.Find(&goodsList)

	goods2 := model.Goods2{
		GoodsName: req.GoodsName,
	}

	db.Where("goods_name like ?", req.GoodsName).First(&goods2)

	if search.GoodsPrice != int64(goods2.GoodsPrice) {
		return &types.Response{
			Code: -1,
			Meg:  "失败",
		}, nil
	}

	return &types.Response{
		Code: 0,
		Meg:  "success",
		Data: map[string]interface{}{
			"search": goods2,
			"list":   goodsList,
		},
	}, nil
}
